草庐IT

第二节: 模块化Module

全部标签

ruby - 我可以在不包含 Ruby 模块的情况下调用实例方法吗?

背景:我有一个模块声明了一些实例方法moduleUsefulThingsdefget_file;...defdelete_file;...defformat_text(x);...end我想从一个类中调用其中一些方法。你通常如何在ruby​​中这样做:classUsefulWorkerincludeUsefulThingsdefdo_workformat_text("abc")...endend问题includeUsefulThings从UsefulThings引入所有方法。在这种情况下,我只需要format_text并且明确不需要get_file和delete_file。我可以看到几

ruby - 在 RSpec 中测试模块

在RSpec中测试模块的最佳实践是什么?我有一些模块包含在少数模型中,现在我只是对每个模型进行重复测试(几乎没有差异)。有没有办法让它干起来? 最佳答案 最好的方式=>let(:dummy_class){Class.new{includeModuleToBeTested}}或者你可以用你的模块扩展测试类:let(:dummy_class){Class.new{extendModuleToBeTested}}在before(:each)中使用'let'比使用实例变量定义虚拟类要好WhentouseRSpeclet()?

ruby - 类和模块的区别

我来自Java,现在我更多地使用Ruby。我不熟悉的一个语言特性是模块。我想知道module到底是什么,什么时候使用它,为什么要在class上使用module? 最佳答案 ╔═══════════════╦═══════════════════════════╦═════════════════════════════════╗║║class║module║╠═══════════════╬═══════════════════════════╬═════════════════════════════════╣║instantia

javascript - 使用模块加载和类继承将 ES6 转换为 ES5

我正在尝试找到将我的ECMAScript6代码转换为ES5的最佳/有效解决方案。我想使用模块加载器并利用继承。到目前为止,我最接近的是使用带有es2015预设和transform-es2015-modules-systemjs插件的Babel6。这是我的.babelrc文件:{"presets":["es2015"],"plugins":["transform-es2015-modules-systemjs"]}我的文件结构如下:-dist(transpiledfilesinthesamestructureasthesrcfolder)-src-classes-Point.js-Col

javascript - 使用 `module` 作为命名空间

我一直在我的node.js模块中使用一种模式,这种模式对我来说非常明显,以至于我认为它一定有问题,否则我会看到更多人这样做。为了保留模块全局的私有(private)变量,我只是将它们作为属性附加到模块对象上。像这样:module.exports={init:function(){module.someClient=initializeSomethingHere()},someMethod:function(done){module.someClient.doSomething(done)}}这对我来说似乎比这样的东西更可取......varsomeClient;module.expor

javascript - 为什么我们需要将 module.exports 作为参数传递,因为我们已经将 module 作为参数传递了?

我一直在阅读一些关于Node.js的在线教程。我的理解是,在使用require(./file-path)函数时,Node获取该文件的内容并包装在一个立即调用的函数中(function(exports,require,module,__filename,__dirname){//content}())我了解exports和module.exports之间的区别。这就是我在互联网上搜索上述问题时所能看到的全部内容。但我的问题是,为什么我们需要将module.exports和module传递给包装IIFE?我们可以单独传递模块,然后从中获取module.exports。这样做有什么好处吗?通

javascript - Sinon - stub 模块功能并在没有依赖注入(inject)的情况下对其进行测试

我有一个代理模块,它将函数调用转发给服务。当调用此代理模块中的函数时,我想测试是否调用了服务函数。这是代理模块:constpayService=require('../services/pay')constwalletService=require('../services/wallet')constentity={chargeCard:payService.payByCardToken,//...someotherfn}module.exports=entity基于thisexample和thisresponse,我试图stub所需的模块“payService”:constexpec

javascript - react : Flow: Import a local js file - cannot resolve issue module

我正在使用Flow:Statictypecheckinglibrary对于React前端应用程序,它会为从src目录的内部导入抛出“无法解析”:Exampleinfileatpath:src/abc/def/ghi/hello.jsx,Iamusingthefollowingimport:importwordsfrom'../words';-->Throwserror"Cannotresolvemodule../wordswords.jsisinsrc/abc/defdir已编辑:安装flow-typed后,我的.flowconfig看起来像这样:[ignore].*/node_mod

javascript - 错误 : "Could not find a declaration file for module ' react-search-input'"

我正在尝试安装react-input-search。我有错误:Couldnotfindadeclarationfileformodule'react-search-input'.'.../app/node_modules/react-search-input/lib/index.js'implicitlyhasan'any'type.Trynpminstall@types/react-search-inputifitexistsoraddanewdeclaration(.d.ts)filecontainingdeclaremodule'react-search-input';ts(70

JavaScript/Dojo 模块模式 - 如何调试?

我正在使用Dojo并使用MasteringDojo中描述的“模块模式”.据我所知,此模式是一种通用且广泛使用的JavaScript模式。我的问题是:我们如何调试我们的模块?到目前为止,我无法说服Firebug向我展示模块的源代码。Firebug似乎只显示用于执行工厂方法的dojoeval语句。因此我无法单步执行我的模块源代码。我试过在我的模块代码中加入“调试器”语句,Firebug似乎正确停止,但不显示源代码。下面是人为设计的示例代码。这只是一个足够复杂的例子,足以使调试的需要变得合理,它并不是有用的代码。页面consoleme@import"../dojoroot/dojo/reso